﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace tpsmi1002
{
    class Controller
    {

        // remplir le listview passe en parametre avec l'integralite des stocks de la bd de frmRechercheAction
        public static void loadListStock(ListView _lv)
        {
            CatalogueStock cs = CatalogueStock.getInstance();

            foreach (KeyValuePair<int, Stock> stock in cs.getListStock())
            {
                ListViewItem row = new ListViewItem(new string[] {stock.Value.Symbol, stock.Value.CompanyName, stock.Value.StockMarket.MarketName});
                row.Tag = stock.Value;
                _lv.Items.Add(row);
            }
        }

        // remplir le listview de frmPortfolio
        public static void loadListTransactions(ListView _lv)
        {
            Portfolio p = Portfolio.getInstance();

            _lv.Items.Clear();

            foreach (KeyValuePair<int, Transaction> t in p.getListTransaction())
            {
                //symbole, compagnie, date, prix, quantity, valeur
                ListViewItem row = new ListViewItem(new string[] { t.Value.Stock.Symbol, t.Value.Stock.CompanyName, t.Value.PurchaseDate.ToString(), String.Format("{0:C}", t.Value.PurchasePrice), t.Value.Quantity.ToString(), String.Format("{0:C}",(t.Value.Quantity * t.Value.PurchasePrice))});
                row.Tag = t.Value;
                _lv.Items.Add(row);
            }
        }

        // sauvegarder une nouvelle transaction a partir de frmAjoutTransaction
        public static void saveNewTransaction(Stock _stock, DateTime _dateAchat, int _quantite, double _prixAchat)
        {
            Portfolio p = Portfolio.getInstance();

            p.addTransaction(_quantite, _stock, _dateAchat, _prixAchat);
        }

        // enregistrer une modification sur une transaction de l'utilisateur
        public static void saveModification(Transaction _transaction, Stock _stock, DateTime _dateAchat, int _quantite, double _prixAchat)
        {
            Portfolio p = Portfolio.getInstance();

            p.saveModification(_transaction, _quantite, _stock, _dateAchat, _prixAchat);
        }

        // vendre des actions contenu dans le portfolio
        public static void sellActions(Transaction _t, double _prixVente, int _qteVente)
        {
            Portfolio p = Portfolio.getInstance();

            p.sellTransaction(_t, _prixVente, _qteVente);
        }

        public static void deleteTransaction(Transaction _t)
        {
            Portfolio p = Portfolio.getInstance();

            p.deleteTransaction(_t);
        }
    }
}
